package cn.edu.sgu.www.mhxysy.service.chongwu_shizhuang.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.chongwu_shizhuang.ChongwuShizhuangDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu_shizhuang.ChongwuShizhuang;
import cn.edu.sgu.www.mhxysy.enums.WearStatus;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.function.computer.score.impl.ChongwuShizhuangScoreComputer;
import cn.edu.sgu.www.mhxysy.mapper.chongwu_shizhuang.ChongwuShizhuangMapper;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.chongwu_shizhuang.ChongwuShizhuangService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ChongwuShizhuangServiceImpl implements ChongwuShizhuangService {

    private final ChongwuShizhuangMapper chongwuShizhuangMapper;
    private final ChongwuShizhuangScoreComputer chongwuShizhuangScoreComputer;

    @Autowired
    public ChongwuShizhuangServiceImpl(
            ChongwuShizhuangMapper chongwuShizhuangMapper,
            ChongwuShizhuangScoreComputer chongwuShizhuangScoreComputer) {
        this.chongwuShizhuangMapper = chongwuShizhuangMapper;
        this.chongwuShizhuangScoreComputer = chongwuShizhuangScoreComputer;
    }

    @Override
    public void compute(String shizhuangId) {
        UpdateWrapper<ChongwuShizhuang> updateWrapper = new UpdateWrapper<>();

        updateWrapper.eq("id", shizhuangId);
        updateWrapper.set("score", chongwuShizhuangScoreComputer.compute(shizhuangId));

        chongwuShizhuangMapper.update(updateWrapper.getEntity(), updateWrapper);
    }

    @Override
    public void wear(String chongwuId, String shizhuangId) {
        ChongwuShizhuang chongwuShizhuang = chongwuShizhuangMapper.selectById(shizhuangId);

        Integer wearStatus = chongwuShizhuang.getWearStatus();

        UpdateWrapper<ChongwuShizhuang> updateWrapper = new UpdateWrapper<>();

        // 宠物装备的状态为0-未穿戴，则操作为穿戴
        if (WearStatus.WCD.getValue().equals(wearStatus)) { // 穿戴
            QueryWrapper<ChongwuShizhuang> queryWrapper = new QueryWrapper<>();

            queryWrapper.eq("chongwu_id", chongwuId);

            Long count = chongwuShizhuangMapper.selectCount(queryWrapper);

            if (count > 0) {
                // 修改宠物原来穿戴的时装状态
                updateWrapper.eq("chongwu_id", chongwuId);
                updateWrapper.eq("wear_status", WearStatus.YCD.getValue());

                updateWrapper.set("chongwu_id", null);
                updateWrapper.set("wear_status", WearStatus.WCD.getValue());

                int update = chongwuShizhuangMapper.update(updateWrapper.getEntity(), updateWrapper);

                if (update == 0) {
                    throw new GlobalException(ResponseCode.CONFLICT, "宠物时装的状态更新异常，请刷新页面后重试！");
                }
            }

            updateWrapper.clear();
            updateWrapper.eq("id", shizhuangId);
            updateWrapper.set("chongwu_id", chongwuId);
            updateWrapper.set("wear_status", WearStatus.YCD.getValue());

            updateWrapper.eq("wear_status", WearStatus.WCD.getValue());

            int update = chongwuShizhuangMapper.update(updateWrapper.getEntity(), updateWrapper);

            if (update == 0) {
                throw new GlobalException(ResponseCode.CONFLICT, "宠物时装的状态更新异常，请刷新页面后重试！");
            }
        } else { // 取下、替换
            // 穿戴装备的宠物ID和参数不一致
            if (!chongwuId.equals(chongwuShizhuang.getChongwuId())) { // 替换
                QueryWrapper<ChongwuShizhuang> queryWrapper = new QueryWrapper<>();

                queryWrapper.eq("chongwu_id", chongwuId);

                Long count = chongwuShizhuangMapper.selectCount(queryWrapper);

                if (count > 0) {
                    // 修改宠物原来穿戴的时装状态
                    updateWrapper.eq("chongwu_id", chongwuId);
                    updateWrapper.eq("wear_status", WearStatus.YCD.getValue());

                    updateWrapper.set("chongwu_id", null);
                    updateWrapper.set("wear_status", WearStatus.WCD.getValue());

                    int update = chongwuShizhuangMapper.update(updateWrapper.getEntity(), updateWrapper);

                    if (update == 0) {
                        throw new GlobalException(ResponseCode.CONFLICT, "宠物时装的状态更新异常，请刷新页面后重试！");
                    }
                }

                // 通过时装ID修改宠物ID
                updateWrapper.clear();
                updateWrapper.set("chongwu_id", chongwuId);

                updateWrapper.eq("id", shizhuangId);
                updateWrapper.eq("wear_status", WearStatus.YCD.getValue());

                chongwuShizhuangMapper.update(updateWrapper.getEntity(), updateWrapper);
            } else { // 取下
                updateWrapper.set("chongwu_id", null);
                updateWrapper.set("wear_status", WearStatus.WCD.getValue());

                updateWrapper.eq("id", shizhuangId);
                updateWrapper.eq("wear_status", WearStatus.YCD.getValue());

                int update = chongwuShizhuangMapper.update(updateWrapper.getEntity(), updateWrapper);

                if (update == 0) {
                    throw new GlobalException(ResponseCode.CONFLICT, "宠物时装的状态更新异常，请刷新页面后重试！");
                }
            }
        }
    }

    @Override
    public void insert(ChongwuShizhuangDTO chongwuShizhuangDTO) {
        ChongwuShizhuang chongwuShizhuang = toEntity(chongwuShizhuangDTO);

        chongwuShizhuang.setId(chongwuShizhuang.uuid());

        if (StringUtils.isNotEmpty(chongwuShizhuangDTO.getChongwuId())) {
            chongwuShizhuang.setWearStatus(WearStatus.YCD.getValue());
        } else {
            chongwuShizhuang.setWearStatus(WearStatus.WCD.getValue());
        }

        chongwuShizhuangMapper.insert(chongwuShizhuang);
    }

    @Override
    public void deleteById(String id) {
        chongwuShizhuangMapper.deleteById(id);
    }

    @Override
    public void updateById(ChongwuShizhuangDTO chongwuShizhuangDTO) {
        ObjectUtils.requireNonNull(chongwuShizhuangDTO.getId(), CustomMessages.ID_NOT_NULL);

        ChongwuShizhuang chongwuShizhuang = toEntity(chongwuShizhuangDTO);

        if (StringUtils.isNotEmpty(chongwuShizhuangDTO.getChongwuId())) {
            chongwuShizhuang.setWearStatus(WearStatus.YCD.getValue());
        } else {
            chongwuShizhuang.setWearStatus(WearStatus.WCD.getValue());
        }

        chongwuShizhuangMapper.updateById(chongwuShizhuang);
    }

    @Override
    public List<ChongwuShizhuang> selectAll() {
        return chongwuShizhuangMapper.selectList(null);
    }

    @Override
    public ChongwuShizhuang selectById(String id) {
        return chongwuShizhuangMapper.selectById(id);
    }

    @Override
    public List<ChongwuShizhuang> selectByChongwuId(Integer chongwuId) {
        if (chongwuId == null) {
            return new ArrayList<>();
        }

        QueryWrapper<ChongwuShizhuang> queryWrapper = new QueryWrapper<>();

        queryWrapper.inSql("category_id",
                "select id from chongwu_shizhuang_category where chongwu_id = " + chongwuId);

        return chongwuShizhuangMapper.selectList(queryWrapper);
    }

    @Override
    public Page<ChongwuShizhuang> selectByPage(Pager<ChongwuShizhuang> pager) {
        Page<ChongwuShizhuang> page = Pager.ofPage(pager);

        return chongwuShizhuangMapper.selectPage(page, null);
    }

    private ChongwuShizhuang toEntity(ChongwuShizhuangDTO chongwuShizhuangDTO) {
        ChongwuShizhuang chongwuShizhuang = new ChongwuShizhuang();

        chongwuShizhuang.setId(chongwuShizhuangDTO.getId());
        chongwuShizhuang.setScore(chongwuShizhuangDTO.getScore());
        chongwuShizhuang.setRoleId(chongwuShizhuangDTO.getRoleId());
        chongwuShizhuang.setChongwuId(chongwuShizhuangDTO.getChongwuId());
        chongwuShizhuang.setCategoryId(chongwuShizhuangDTO.getCategoryId());

        return chongwuShizhuang;
    }

}